package org.gitos.cloud.domain.server;

import org.gitos.cloud.domain.mq.MqMsg;
import org.gitos.cloud.domain.tcp.Bucket;
import org.gitos.cloud.util.HexUtil;
import org.springframework.jdbc.core.RowMapper;

import java.io.Serializable;
import java.net.InetSocketAddress;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

/**
 * Created by hc on 2016/3/29 0029.
 */
public class Led implements Serializable, RowMapper<Led> {
    private String uuid;
    private String mac;
    private String wifi_ver;
    private String mcu_ver;
    private String nat_ip;
    private String pub_ip = "";
    private String ssid;
    private String wifi_psw;
    private String user_uuid;
    private boolean is_online;

    public boolean is_online() {
        return is_online;
    }

    public void setIs_online(boolean is_online) {
        this.is_online = is_online;
    }

    public String getUser_uuid() {
        return user_uuid;
    }

    public void setUser_uuid(String user_uuid) {
        this.user_uuid = user_uuid;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public String getWifi_ver() {
        return wifi_ver;
    }

    public void setWifi_ver(String wifi_ver) {
        this.wifi_ver = wifi_ver;
    }

    public String getMcu_ver() {
        return mcu_ver;
    }

    public void setMcu_ver(String mcu_ver) {
        this.mcu_ver = mcu_ver;
    }

    public String getNat_ip() {
        return nat_ip;
    }

    public void setNat_ip(String nat_ip) {
        this.nat_ip = nat_ip;
    }

    public String getPub_ip() {
        return pub_ip;
    }

    public void setPub_ip(String pub_ip) {
        this.pub_ip = pub_ip;
    }

    public String getSsid() {
        return ssid;
    }

    public void setSsid(String ssid) {
        this.ssid = ssid;
    }

    public String getWifi_psw() {
        return wifi_psw;
    }

    public void setWifi_psw(String wifi_psw) {
        this.wifi_psw = wifi_psw;
    }

    public Led(MqMsg msg) throws Exception {
        Bucket bucket = msg.getBucket();
        byte[] bytes = bucket.getContent();
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F'};
        String mac = HexUtil.encodeHexStr(bucket.getDeviceId().getValue());
        if (bytes.length != 92 || "".equals(mac)) {
            throw new Exception("设备信息不正确");
        }
        this.nat_ip = new String(Arrays.copyOfRange(bytes, 0, 14));
        this.ssid = new String(Arrays.copyOfRange(bytes, 16, 47));
        this.wifi_psw = new String(Arrays.copyOfRange(bytes, 48, 79));
        this.wifi_ver = new String(Arrays.copyOfRange(bytes, 80, 85));
        this.mcu_ver = new String(Arrays.copyOfRange(bytes, 86, 91));
        this.mac = mac;
        this.pub_ip =msg.getPubIp();
    }

    public Led() {
    }

    @Override
    public Led mapRow(ResultSet rs, int rowNum) throws SQLException {
        Led Led = new Led();
        Led.setMac(rs.getString("mac"));
        Led.setMcu_ver(rs.getString("mcu_ver"));
        Led.setNat_ip(rs.getString("nat_ip"));
        Led.setPub_ip(rs.getString("pub_ip"));
        Led.setWifi_ver(rs.getString("wifi_ver"));
        Led.setUuid(rs.getString("uuid"));
        Led.setSsid(rs.getString("ssid"));
        Led.setWifi_psw(rs.getString("wifi_psw"));
        Led.setUser_uuid(rs.getString("user_uuid"));
        return Led;
    }
}
